﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       TaskTool
- 机器名称：       YTE
- 命名空间：       LD.Tool.Other
- 文 件 名：       TaskTool
- 创建时间：       2022/6/14 12:03:04
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.BC;
using LD.Host;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace LD.Tool
{
    [Tool(FrmParam = "ToolIfFrm", Text = "逻辑判断", ToolType = ToolEnum.Other)]
    public class ToolIf : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            stopwatch.Start();
            try
            {
                if (!string.IsNullOrEmpty(model.Param))
                {
                    var currentState = Appbus.StateEnum;
                    var models = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ToolIfParam>>(model.Param);
                    foreach (var item in models)
                    {
                        //判定运行前状态，否则进程无法结束
                        if (currentState != Appbus.StateEnum)
                            break;
                        //暂时只是支持同步模式
                        ExeIF(item);
                    }
                    isSuccess = true;
                }
                else
                    message = "任务未指定";
            }
            catch (Exception ex)
            {
                isSuccess = false;
                message = ex.Message;
            }
            finally
            {
                stopwatch.Stop();
            }
            return new OpResult() { IsSuccess = isSuccess, Message = "", ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
        private void ExeIF(ToolIfParam toolIfParam)
        {
            if (toolIfParam.ToolIfMode == ToolIfMode.全局变量)
            {
                var cacheVal = Cache.Var
                            .Where(p => p.Name == toolIfParam.Variable)
                            .FirstOrDefault();
                if (cacheVal == null ||
                    cacheVal.Val == null ||
                    cacheVal.Val.ToString() == "")
                    return;

                //判定条件
                switch (toolIfParam.OpChar)
                {
                    case OpChar.大于:
                        if (Convert.ToInt32(cacheVal.Val) > Convert.ToInt32(toolIfParam.OpVar))
                        {
                            ExceTask(toolIfParam.FlowName);
                        }
                        break;
                    case OpChar.大于等于:
                        if (Convert.ToInt32(cacheVal.Val) >= Convert.ToInt32(toolIfParam.OpVar))
                        {
                            ExceTask(toolIfParam.FlowName);
                        }
                        break;
                    case OpChar.等于:
                        DataType dataType = (DataType)Enum.Parse(typeof(DataType), cacheVal.DataType);
                        //分类型
                        switch (dataType)
                        {
                            case DataType.Int:
                                if (Convert.ToInt32(cacheVal.Val) == Convert.ToInt32(toolIfParam.OpVar))
                                    ExceTask(toolIfParam.FlowName);
                                break;
                            case DataType.Bool:
                                if (Convert.ToBoolean(cacheVal.Val) == Convert.ToBoolean(toolIfParam.OpVar))
                                    ExceTask(toolIfParam.FlowName);
                                break;
                            case DataType.Double:
                                if (Convert.ToDouble(cacheVal.Val) == Convert.ToDouble(toolIfParam.OpVar))
                                    ExceTask(toolIfParam.FlowName);
                                break;
                            case DataType.Float:
                                if (Convert.ToSingle(cacheVal.Val) == Convert.ToSingle(toolIfParam.OpVar))
                                    ExceTask(toolIfParam.FlowName);
                                break;
                            default:
                                if (cacheVal.Val.ToString() == toolIfParam.OpVar.ToString())
                                    ExceTask(toolIfParam.FlowName);
                                break;
                        }

                        break;
                    case OpChar.小于等于:
                        if (Convert.ToInt32(cacheVal.Val) <= Convert.ToInt32(toolIfParam.OpVar))
                        {
                            ExceTask(toolIfParam.FlowName);
                        }
                        break;
                    case OpChar.小于:
                        if (Convert.ToInt32(cacheVal.Val) < Convert.ToInt32(toolIfParam.OpVar))
                        {
                            ExceTask(toolIfParam.FlowName);
                        }
                        break;
                    default:
                        break;
                }
            }
            else if (toolIfParam.ToolIfMode == ToolIfMode.IO)
            {
                var io = BcFactory.Instance.Config.IoList
                              .Where(p => p.IoType == (int)toolIfParam.IoType &&
                                          p.Name == toolIfParam.IoName)
                              .FirstOrDefault();
                if (io != null)
                {
                    int low_hig;
                    if (toolIfParam.IoType == (int)IoType.输入)
                        low_hig = BcFactory.Instance.Control.GetIn(io.No);
                    else
                        low_hig = BcFactory.Instance.Control.GetOut(io.No);

                    if (low_hig == (int)toolIfParam.LowHig)
                    {
                        ExceTask(toolIfParam.FlowName);
                    }
                }
            }
        }
        private void ExceTask(string flowName)
        {
            var flow = Appbus.PartNo.Config.Flow
                        .Where(p => p.Name == flowName && p.FlowType == (int)FlowType.流程)
                        .OrderBy(p => p.Sort)
                        .FirstOrDefault();
            var task = FlowHelper.Add(flow, true);
            task?.Wait();
        }
    }
}
